package com.ruoyi.web.controller.mall;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallZone;
import com.ruoyi.mall.service.MallZoneService;
import com.ruoyi.system.service.ISysDictDataService;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品所属专区Controller
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@Api(tags = "商品所属专区")
@RestController
@RequestMapping("/mall/zone")
public class MallZoneController extends BaseController {
    @Autowired
    private MallZoneService mallZoneService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询商品所属专区列表
     */
    @ApiOperation("查询商品所属专区列表")
    @PreAuthorize("@ss.hasPermi('mall:zone:list')")
    @PostMapping("/list")
    public TableDataInfo list(MallZone mallZone) {
        startPage();
        List<MallZone> list = mallZoneService.selectMallZoneList(mallZone);
        if (CollectionUtils.isNotEmpty(list)) {
            List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(new SysDictData("pay_method", "0"));
            Map<String,  String> dictDataMap = dictDataList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
            list.forEach(u -> {
                String str = u.getPayIntegral();
                if (StringUtils.isNotBlank(str)) {
                    String[] arr = str.split(",");
                    List<String> stringList = new ArrayList<>();
                    for (String s : arr) {
                        stringList.add(dictDataMap.get(s));
                    }
                    u.setPayIntegralStr(StringUtils.join(stringList, ","));
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 获取商品所属专区选择框列表
     */
    @ApiOperation("获取商品所属专区选择框列表")
    @PreAuthorize("@ss.hasPermi('mall:zone:list')")
    @PostMapping("/select")
    public AjaxResult select() {
        return success(mallZoneService.selectMallZoneList(null));
    }

    /**
     * 获取商品所属专区详细信息
     */
    @ApiOperation("获取商品所属专区详细信息")
    @PreAuthorize("@ss.hasPermi('mall:zone:query')")
    @PostMapping(value = "/getInfo/{zoneId}")
    public AjaxResult getInfo(@PathVariable("zoneId") Long zoneId) {
        return success(mallZoneService.selectMallZoneByZoneId(zoneId));
    }

    /**
     * 保存商品所属专区
     */
    @ApiOperation("保存商品所属专区")
    @PreAuthorize("@ss.hasPermi('mall:zone:update')")
    @Log(title = "商品所属专区", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public AjaxResult update(@RequestBody MallZone mallZone) {
        if (mallZone.getZoneId() == null) {
            return toAjax(mallZoneService.insertMallZone(mallZone));
        } else {
            MallZone dbObj = mallZoneService.selectMallZoneByZoneId(mallZone.getZoneId());
            Assert.notNull(dbObj, "数据不存在，无法更新");
            BeanUtils.copyProperties(mallZone, dbObj, "zoneId", "createTime");
            return toAjax(mallZoneService.updateMallZone(dbObj));
        }
    }

    /**
     * 删除商品所属专区
     */
    @ApiOperation("删除商品所属专区")
    @PreAuthorize("@ss.hasPermi('mall:zone:remove')")
    @Log(title = "商品所属专区", businessType = BusinessType.DELETE)
    @PostMapping("remove/{zoneIds}")
    public AjaxResult remove(@PathVariable Long[] zoneIds) {
        return toAjax(mallZoneService.deleteMallZoneByZoneIds(zoneIds));
    }
}
